<?php

namespace Caifenglei\AuthRbac\Http\Controllers;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, ValidatesRequests;

    /**
     * 成功的返回
     * @param $message
     * @param array $data
     * @return \Illuminate\Http\JsonResponse
     */
    public function success($message, array $data = []) {

        return response()->json([
            'code' => 200,
            'msg' => $message,
            'time' => time(),
            'data' => $data
        ]);
    }

    /**
     * 失败的返回
     * @param $message
     * @param $data
     * @return \Illuminate\Http\JsonResponse
     */
    public function fail($message) {
        return response()->json([
            'code' => 400,
            'msg' => $message,
            'time' => time()
        ]);
    }

    /**
     * 返回统一的分页数据
     * @param LengthAwarePaginator $paginator
     * @return array
     */
    public function formatPaginate(LengthAwarePaginator $paginator) {
        return [
            'total' => $paginator->total(),
            'data' => $paginator->items(),
            'from' => $paginator->firstItem(),
            'to' => $paginator->lastItem(),
        ];
    }
}
